// map.js
import qqmap from '../../../utils/qqmapSdk';
Page({
  data: {
    latitude: 32.011067,
    longitude: 112.13423,
    scale:16,
    markers: [
      {
        id: 1,
        latitude: 32.011067,
        longitude: 112.13423,
        width: 40,
        height: 40,
        
      },
      {
        id: 2,
        latitude: 32.011167,
        longitude: 112.13121,
        iconPath: '/static/images/marker.png',
        width: 40,
        height: 40,
      },
    
    ],
     // 路线规划
     polyline: []
  },
  onLoad(query) {
    
    this.getPoyline()
     // 根据报修 id 获取报修详情
     this.getDetail(query.id)
  },


  // 地图路线绘制
  getPoyline() {    
    qqmap.direction({
      mode: 'bicycling',
      from: {latitude: 32.011067, longitude: 112.13423},
      to: {latitude: 32.011167, longitude: 112.13121},
      success: (res) => {
        const coors = res.result.routes[0]?.polyline
       
       //坐标解压（返回的点串坐标，通过前向差分进行压缩）
       for (let i = 2; i < coors.length; i++) {
        coors[i] = coors[i - 2] + coors[i] / 1000000
      }
      const points = []
      // 获取经纬度
      for (let i = 0; i < coors.length; i += 2) {
        points.push({ latitude: coors[i], longitude: coors[i + 1] })
      }
        
        this.setData({
          polyline:[
            {
              points,
              color:"#00d26a",
              width:4,
            }
          ]
        })
      }
    })
  },
  // 获取报修详情
  async getDetail (id) {
    const res = await wx.http.get(`/repair/${id}`)
    this.setData({
      detail: res.data
    })
  },
  // 取消报修
  async cancelRepair(ev) {
    // 二次确认
    const { confirm } = await wx.showModal({
      content: '确定取消报修吗？',
      cancelText: '点错了',
      cancelColor: '#999'
    })
    // 确认取消
    if (confirm) {
      // 调用取消报修的接口
      await wx.http.put(`/cancel/repaire/${ev.mark.id}`)
      // 跳转到报修列表页面
      wx.redirectTo({
        url: '/repair_pkg/pages/list/index'
      })
    }
  }
})
